package com.example.alarmclockdemo

import android.annotation.SuppressLint
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.fandou.airobot.R
import com.fandou.airobot.database.Record
import kotlinx.android.synthetic.main.ai_robot_record_history_item_layout.view.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author       Jm
 * @date         2020/7/9 16:52
 * @description  历史记录adapter
 */

class RecordHistoryAdapter() : BaseQuickAdapter<Record, BaseViewHolder>(R.layout.ai_robot_record_history_item_layout) {

    private var mScreenWidth: Int = 0;

    init {
        mScreenWidth = ScreenUtils.getScreenWidth()
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder?, item: Record?) {

        helper!!.addOnClickListener(R.id.ivPlay)
//        helper.addOnClickListener(R.id.csbSend)
//        helper.addOnClickListener(R.id.csbDelete)

        helper.itemView.run {

            csbCreateData.text = TimeUtils.millis2String(item!!.timestamp, SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()))
            Glide.with(mContext).load(item.coverPath).into(rivCover)
        }
    }


    /**
     * delete an record by id
     */
    fun deleteRecord(id: Long) {
        remove(data.indexOf(data.find { it.id == id }))
    }

    /**
     * Update the alarm
     */
    fun updateAlarm(record: Record) {
        setData(data.indexOf(data.find { it.id == record.id }), record)
    }
}